#if _IOS_

#include "MioPlatform_iOS.h"

/*
#define LOG(format, ...)	\
CFShow([NSString stringWithFormat:[NSString stringWithUTF8String:format], ## __VA_ARGS__]);
//워닝안뜨는 한줄로그
#define LINE_LOG(msg)	CFShow([NSString stringWithUTF8String:msg]);
*/

using std::string;

namespace mio
{
	void Console_iOS::timelog(const std::string &msg) const
	{
		NSLog(@"%s", msg.c_str());
	}
	void Console_iOS::timelog(const char *fmt, ...) const
	{
		/*
		//설마 에러가 이거보다 길진 않겠지?...
		const int bufsize = 1024 * 64;
		char buffer[bufsize];

		//가변인자 변환
		va_list argptr;
		int ret;
		va_start(argptr, fmt);
		ret = sprintf(buffer, fmt, argptr);
		va_end(argptr);
		std::string msg(buffer);
		 */
		FMT_TO_STRING(fmt, ...);
		timelog(msg);
	}

	void Console_iOS::log(const std::string &msg) const
	{
#define LINE_LOG(msg)	CFShow([NSString stringWithUTF8String:msg.c_str()]);
		LINE_LOG(msg);
#undef LINE_LOG
	}
	void Console_iOS::log(const char *fmt, ...) const
	{
		FMT_TO_STRING(fmt, ...);
		log(msg);
//#define LOG(format, ...)	\
//CFShow([NSString stringWithFormat:[NSString stringWithUTF8String:format], ## __VA_ARGS__]);
//		LOG(fmt, ...);
//#undef LOG
	}


#pragma mark path implement
	Path& getPath()
	{
		static Path_iOS path;
		return path;
	}
	std::string Path_iOS::docPath(const std::string &file)
	{
		NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
		NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
		NSString *docDir = [paths objectAtIndex:0];

		NSString *nsfilename = [NSString stringWithCString:file.c_str() encoding:NSUTF8StringEncoding];
		NSString *path = [docDir stringByAppendingPathComponent:nsfilename];
		std::string result = [path cStringUsingEncoding:NSUTF8StringEncoding];
		[pool drain];
		return result;
	}

	std::string Path_iOS::appPath(const std::string &file)
	{
		NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
		NSString *filenameStr = [NSString stringWithCString:file.c_str() encoding:NSUTF8StringEncoding];
		NSString *path = [[NSBundle mainBundle] pathForResource:filenameStr ofType:nil];

		//파일이 존재하면 path는 문자열이지만 없으면 nil이다. 적절히 분기해서 처리하기
		const char *result = NULL;
		if([path length] == 0)
		{
			result = "";
		}
		else
		{
			result = [path cStringUsingEncoding:NSUTF8StringEncoding];
		}
		string resultStr(result);
		[pool drain];
		return resultStr;
	}

	bool Path_iOS::isFileExist(const std::string &file)
	{
		NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
		NSString *path = [NSString stringWithCString:file.c_str() encoding:NSUTF8StringEncoding];
		BOOL exist = [[NSFileManager defaultManager] fileExistsAtPath:path];
		[pool drain];
		if(exist == YES)
			return true;
		else
			return false;
	}
}
#endif
